<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>数组遍历的方法</title>
</head>
<body>
    

    <script>

        /* var obj = {name: 'wangshuo'};
        var ary = [1,2,3,4,5,6]; */
        // ary.forEach(function(){
        //     console.log(this);
        // }.call(obj)); // 给 foreach赋值的时候，首先把匿名函数中的this变成obj，并且函数立即执行，由于函数中没有return，所以是undefined，不是函数。
        // ary.forEach(function(){
        //     console.log(this);
        // }.bind(obj));

        // ary.forEach(function(item,index,arr){
        //     console.log(arr);
        // },obj); 

        // 兼容的foreach
      /*  Array.prototype.myForeach = function (callback,context) {  
            context = context || window;
            if("foreach" in Array.prototype){
                this.forEach(callback,context);
                return;
            }
            // ie 6 -8 
            
        }  */

        /*var arr = ary.map((item,index)=>{
            var i = item*10;
            return i;
        });

        console.log(arr);*/

        // find 返回符合条件的第一个元素的值

        let ary = ["name","age","title","nnn"];

        let result = ary.find(item=>{
            return item.indexOf('n') > -1
        });

        console.log(result);

        // find 和 filter 都是返回符合条件的元素 区别是 find 只返回符合条件的第一个元素 而filter返回所有符合条件的元素的数组

        // every some find findIndex map filter forEach reduce

        // -> some
        let a = ["a","b","c","d"];

        let res = a.some(item=>{
            return item.indexOf('a')>-1;
        });

        console.log(`res(some) -- ${res}`);

        // -> every
        let b = ["apple","banana","cat","da"];

        let res1 = b.every(item=>{
            return item.indexOf('b')>-1;
        });

        console.log(`res1(every) -- ${res1}`);
        
        // -> find 
        let c = ["abandon","black","computer","demo","extend","fuck"];

        let res2 = c.find(item=>{
            return item.includes('aban');    
        });

        console.log(`res2(find) -- ${res2}`);

        // -> filter 

        let d = [1,2,3,4,5,6,7,8,9];

        let res3 = d.filter(item=>{
            return item>5
        });

        console.log(`res3(filter) -- ${res3}`);

        // -> findIndex

        let e = [
            {
                id: 1,
                name: 'ww'
            },
            {
                id: 2,
                name: 'cc'
            },
            {
                id: 3,
                name: 'ss'
            }
        ];

        let res4 = e.findIndex(item=>{
            return item.id === 3
        });

        console.log(`res4(findIndex) -- ${res4}`);


        // -> map

        let h = [1,2,3,4,5,6];

        let new_h = h.map(item=>{
            return item*10;
        });

        console.log(`h(new_h) -- ${new_h}`);
        console.log(toString.call(new_h)); 

        // forEach 

        let num = 0;

        let f = [1,2,3,4,5,6];

        f.forEach(item=>{
            num += item;
        });

        console.log(`num(forEach) -- ${num}`);

        // reduce

        let g = [1,2,3,4,5];

        let sum = g.reduce((prev,next)=>{
            return prev + next;
        });

        console.log(`sum(reduce) -- ${sum}`);

    </script>

</body>
</html>